/************************************************************************/
/* Game                                                                 */
/************************************************************************/
#include "Game/Animations/StandardAnimationSet.h"

namespace TD_Game {


   /************************************************************************/
   /* Animation set initialization                                         */
   /************************************************************************/
   bool StandardAnimationSet::initAnimations(unsigned int g_selector) {

      return true;
   }

   /************************************************************************/
   /* ctors and dtors                                                      */
   /************************************************************************/

   /*StandardAnimationSet::StandardAnimationSet() : AnimationSet() {

   }*/
   StandardAnimationSet::StandardAnimationSet(const string &startingAnimation,std::string the_xml,std::string the_png,unsigned int the_line) : AnimationSet(startingAnimation) {
	   m_pXMLDoc = TD_Resources::ResourceManager::loadXMLDocument(the_xml.c_str());
      /*if(!m_pXMLDoc->LoadFile())
      {
         cout << "falho abrir" <<endl;
         return;
      }*/
      TiXmlElement *elementSearch, *animationSearch;
      elementSearch = m_pXMLDoc->FirstChildElement("AnimationSet");
      if(!elementSearch)
      {
         cout << "sem <AnimationSet>" << endl;
         return;
      }
      elementSearch=elementSearch->FirstChildElement("TileSize");
      if(!elementSearch)
      {
         cout << "sem <TileSet>" << endl;
         return;
      }
      if(!(elementSearch->Attribute("x") && elementSearch->Attribute("y")))
      {
         cout << "TileSize sem valor" << endl;
         return;
      }

      stringstream straux;
      unsigned int x,y;
      straux << elementSearch->Attribute("x");
      straux >> x;
      straux.clear();
      straux << elementSearch->Attribute("y");
      straux >> y;


      m_pSprites =  TD_Resources::ResourceManager::loadCharacterTileset(the_png.c_str(), Coord2D(x*screenWidth/1024.0,y*screenHeight/768.0), Color::PINK);
      int marioQuantity = m_pSprites->getTileImg()->getBounds().x/(x*screenWidth/1024.0);
      int defaultWalkingInterval=100;
      bool loop=true;


      //Animation Sub
      elementSearch=elementSearch->NextSiblingElement("Animations");
      animationSearch=elementSearch->FirstChildElement();

      const char *result;

      while(animationSearch)
      {
         result = getAnimation(animationSearch,animationSearch->Value(),&loop,&defaultWalkingInterval);
         if(result)
         {
            Animation *p_animation = new Animation(animationSearch->Value(), defaultWalkingInterval,loop);
            stringstream resultstr;
            resultstr << result;
            unsigned int tmp;
            while((resultstr >> tmp))
            {
               p_animation->addAnimationTile(m_pSprites->getTile(tmp + the_line * marioQuantity));
            }
            string auxstr=animationSearch->Value();
            deleteAnimation(auxstr);
            m_animationMap[auxstr] = p_animation;
         }
         animationSearch=animationSearch->NextSiblingElement();
      }
   }

   StandardAnimationSet::StandardAnimationSet(const StandardAnimationSet &rhs) : AnimationSet(rhs) {

   }

   StandardAnimationSet & StandardAnimationSet::operator =(const StandardAnimationSet &rhs) {
      AnimationSet::operator =(rhs);
      return(*this);
   }

   StandardAnimationSet::StandardAnimationSet(const string &startingAnimation, unsigned int g_selector) 
      : AnimationSet(startingAnimation) {
         initAnimations(g_selector);
   }
   StandardAnimationSet::~StandardAnimationSet() {

   }

   /************************************************************************/
   /* Aux functions                                                        */
   /************************************************************************/

   const char* StandardAnimationSet::getAnimation(TiXmlElement *animElement, const char *name, bool *loop, int* interval)
   {
      stringstream auxstream;
      const char *aux;
      if(!animElement)
         return NULL;
      if(loop)
      {
         auxstream.clear();
         aux = animElement->Attribute("loopedAnimation");
         if(aux)
         {
            auxstream << aux;
            auxstream >> *loop;
         }
         else
            *loop=true;
      }
      if(interval)
      {
         auxstream.clear();
         aux = animElement->Attribute("animationInterval");
         if(aux)
         {
            auxstream << aux;
            auxstream >> *interval;
         }
         else
            *interval=100;
      }
      return animElement->GetText();
   }


}